<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>ai聊天室</title>
  <link rel="stylesheet" href="/static/css/body.css">
  <link rel="shortcut icon" href="/static/imag/favicon.ico" type="image/x-icon" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>
  <style>
    .notification-center {
      /* justify-content: center; */
      margin-top: 20px;
      padding: 20px;
      background-color: var(--notification-bg-color);
      border-radius: 8px;
    }

    .notification-center h2 {
      font-size: 1.2em;
      /* 缩小字号 */
      display: inline-block;
      /* 使背景仅在文字上 */
      background-color: #28a745;
      /* 鲜绿色背景 */
      padding: 5px 10px;
      border-radius: 5px;
      margin-bottom: 20px;
    }

    .notification-list {
      height: 400px;
      overflow-y: auto;
      border: 1px solid #ccc;
      padding: 10px;
      background-color: var(--notification-list-bg-color);
      color: #fff;
      min-height: 70vh;

    }

    .notification-item {
      margin: 10px 0;
      padding: 10px;
      padding-top: 20px;
      background-color: var(--notification-item-bg-color);
      border-radius: 5px;
      /* color: var(--notification-item-system-text-color); */
      /* display: flex; */
      justify-content: space-between;
      align-items: center;
      word-wrap: break-word;
      position: relative;
    }

    .notification-item .timestamp {
      font-size: 0.7em;
      /* 时间显示字体大小 */
      color: #ccc;
      /* 时间显示颜色 */
      margin-left: 10px;
      /* 时间显示位置 */
      position: absolute;
      left: 3px;
      top: 2px;
    }

    /* 按钮样式 */
    .send-message {
      display: flex;
      justify-content: center;
      margin-top: 50px;
      /* 扩大聊天框区域 */
    }

    .send-message #message-input {
      width: 70%;
      padding: 10px;
      border-radius: 5px;
      border: none;
      margin-right: 10px;
    }

    .send-message #send-button {
      padding: 10px 20px;
      background-color: var(--button-bg-color);
      border: none;
      border-radius: 5px;
      cursor: pointer;
      transition: background-color 0.3s ease;
    }

    .send-message #send-button:hover {
      background-color: #ff9a5a;
    }
  </style>
  <script>
    localStorage.clear();
    function play_audio(data) {
      console.log(data)
    }
    function text_to_speech(text) {
      console.log(text)
      //通过fectch发给后端
      fetch('/text_to_speech', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          text: text
        })
      })
        .then(response => {
          return response.blob();
        })
        .then(au_data => {
          const url = URL.createObjectURL(au_data);
          const audio = new Audio(url);
          audio.play();
          md5Hash = CryptoJS.MD5(text).toString();
          localStorage.setItem(md5Hash + '.mp3', url);
        })
        .catch(error => console.error(error))
    }

    function send_text_to_speech(spanElement) {
      parent = spanElement.parentNode;
      parent = parent.parentNode;
      text = ""
      parent.childNodes.forEach(node => {
        if (node.nodeType === Node.TEXT_NODE) {
          text += node.textContent.trim(); // 收集文本并去除首尾空白
        }
      });
      text = text.replace(/.*?: /, '');
      const md5Hash = CryptoJS.MD5(text).toString();
      console.log(md5Hash)
      if (localStorage.getItem(md5Hash + '.mp3')) {
        const audio = new Audio(localStorage.getItem(md5Hash + '.mp3'));
        audio.play();
        console.log('use cache')
        return;
      } else {
        text_to_speech(text)
      }
    }
  </script>
</head>

<body onunload="msg_init()">
  <div class="container">
    <!-- 侧边栏 -->
    <div class="sidebar">
      <h2>导航栏</h2>
      <a href="#" onclick="go_back()">返回首页</a>
      <a href="#" onclick="open_win()">窗户状态</a>
      <a href="#" onclick="open_msg()">通知中心</a>
      <a href="#" onclick="open_ai_chat()">ai聊天</a>
      <a href="#" onclick="open_audio()">语音提醒</a>
      <a href="#" onclick="open_lost_and_found()">失物招领中心</a>
      <a href="#" onclick="open_car_video()">小车视频</a>
    </div>

    <!-- 主内容区 -->
    <div class="main-content">
      <div class="header">
        <img src="./static/imag/logo.png" alt="Logo" class="logo" title="点击切换主题" id="logo_switchTheme">
        <h1>ai聊天室</h1>
        <div class="user-info">
          <img src="./static/imag/adm.jpg" alt="User Avatar">
          <div class="invite-code-popup">
            用户: <span id="uname">{{.username}}</span>
            <br>邀请码: {{.invitecode}}(禁止外传)
          </div>
          <div class="username">用户: {{.username}}</div>
          <button class="logout-button" onclick="logout()">退出登录</button>
        </div>
      </div>

      <div class="notification-center">
        <h2 id="net_status">网络已连接</h2>
        <div class="notification-list" id="messages">
          {{range .history_msg}}
          {{if eq .Name "ai"}}
          <div class="notification-item" style="color:var(--notification-item-system-text-color);">
            {{else}}
            <div class="notification-item" style="color:var(--notification-item-user-text-color);">
              {{end}}
              {{.Name}}: {{.Msg}}
              <span class="timestamp">{{.Time}} <span
                  style="background-color: #ff6b6b ; color: #fff;border-radius: 2px; padding-top: 2px;padding-left: 2px;padding-right: 2px;"
                  onclick="send_text_to_speech(this)">播放</span></span>
            </div>
            {{end}}
          </div>
        </div>

        <!-- 输入框和发送按钮 -->
        <form class="send-message" id="message_form">
          <input type="text" placeholder="输入消息..." id="message-input">
          <input type="submit" value="发送" id="send-button">
        </form>
      </div>

    </div>

    <!-- 底部版权信息 -->
    <div class="footer">
      多模态智能校园管理平台
    </div>
    <script src="/static/js/route.js"></script>
    <script src="/static/js/ai_chat.js"></script>
</body>

</html>